package com.tg.app.activity.account;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.appbase.custom.constant.CommonConstants;
import com.tg.app.R;
import com.base.BaseActivity;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;
import com.tange.base.toolkit.PreferenceUtil;

import com.tg.data.http.entity.ModifyNickNameBean;


import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;


public class ChangeNickNameActivity extends BaseActivity implements View.OnClickListener {


    private EditText nameEditText;
    private String oldNickName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_change_nick_name);
        hideActionBar();
        oldNickName = PreferenceUtil.getString(getBaseContext(), CommonConstants.PRE_USER_NICK_NAME);
        initView();
    }

    protected void initView() {
        RelativeLayout toolbar = findViewById(R.id.toolbar);
        TextView title = toolbar.findViewById(R.id.device_name);
        ImageButton back = toolbar.findViewById(R.id.back_toolbar);
        nameEditText = findViewById(R.id.change_nick_name_edit_text);
        final ImageButton clear = findViewById(R.id.delete_edit_text);
        nameEditText.setText(oldNickName);
        if (!TextUtils.isEmpty(nameEditText.getText().toString())) {
            clear.setVisibility(View.VISIBLE);
        }
        nameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s)) {
                    clear.setVisibility(View.VISIBLE);
                } else {
                    clear.setVisibility(View.GONE);
                }
            }
        });
        TextView save = toolbar.findViewById(R.id.toolbar_select);
        save.setOnClickListener(this);
        back.setOnClickListener(this);
        title.setText(R.string.set_nick_name);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                nameEditText.setText("");
            }
        });

    }

    @Override
    public boolean shouldSetStatusBarColor() {
        return true;
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        showInput(nameEditText);
        String  name = nameEditText.getText().toString().trim();
        if (name.length()>0){
            nameEditText.setSelection(name.length());
        }
    }


    private void showInput(EditText et) {
        et.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams
                .SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }


    @Override

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.back_toolbar) {
            this.finish();
        } else if (id == R.id.toolbar_select) {
            String name = nameEditText.getText().toString().trim();
            if (TextUtils.equals(name, oldNickName)) {
                finish();
            } else {
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(ChangeNickNameActivity.this, R.string.change_nick_name_is_empty, Toast.LENGTH_SHORT).show();
                } else {
                    setName(nameEditText.getText().toString().trim());
                }

            }
        }
    }

    private void setName(String name) {
        showLoading();
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("nickname",name);
        TGHttp.getInstance().modifyNickName(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ClientObserver<ModifyNickNameBean>() {

                    @Override
                    protected void onSuccess(ModifyNickNameBean content) {
                        Toast.makeText(ChangeNickNameActivity.this, R.string.modify_success, Toast.LENGTH_SHORT).show();
                        PreferenceUtil.setString(getBaseContext(), CommonConstants.PRE_USER_NICK_NAME, nameEditText.getText().toString());
                        ChangeNickNameActivity.this.finish();
                    }

                    @Override
                    protected void onResponseError(int errorCode, String errorInfo) {
                        Toast.makeText(ChangeNickNameActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onOtherError(String error) {
                        Toast.makeText(ChangeNickNameActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    protected void onFinish() {
                        hideLoading();
                    }
                });
    }
}
